<?php

namespace App\Services\ReceiveProductOrder;

use Carbon\Carbon;
use App\Models\Order;
use App\Events\OrderReceivedEvent;
use Illuminate\Support\Facades\DB;

class ReceiveProductOrder
{
	/**
	 * @var Order
	 */
	protected $order;
	
	public function __construct(Order $order)
	{
		$this->order = $order;
	}
	
	protected function updateOrder()
	{
		DB::transaction(function () {
			$this->order->update([
				'delivery_status' => 'received',
				'received_at' => Carbon::now()
			]);
			$this->order->items()->update([
				'delivery_status' => 'received'
			]);
			event(new OrderReceivedEvent($this));
		});
	}
	
	public function receive()
	{
		$this->updateOrder();
		return $this->order;
	}
}
